<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="renderer" content="webkit|ie-comp|ie-stand">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<#include "lib/lib.html" />
<script type="text/javascript">
</script>
<title>分配权限</title>
</head>
<body>
<div class="pd-20">
	<form action="" method="post" class="form form-horizontal" id="form-role-permiss">
		<input type="hidden" name="chId" id="chId" value="${obj.id!}"/>
		
		<!-- 权限列表 -->
		<div class="row cl">
			<label class="form-label col-2">权限列表：</label>
			<div class="formControls col-10">
					<dl class="permission-list">
						<dt>
							<label> 
								<input type="checkbox" value="" name="user-Character-0" id="user-Character-0">
								${obj.channelName!}
							</label>
						</dt>
					<dd>
						<#if funList?? && funList?size gt 0 >
						<#list funList as fl >
							<#if fl.fid == -1>
							<dl class="cl permission-list2">
								<dt class="funroles">
									<label class="" style="color:#5a98de; font-size:14px;">
										├ 
										<#if fl.checked??>
										<input type="checkbox" value="${fl.id!}" name="user-menu-${fl_index!}" id="user-menu-${fl_index!}"  checked="checked" />
										<#else>
										<input type="checkbox" value="${fl.id!}" name="user-menu-${fl_index!}" id="user-menu-${fl_index!}"  />
										</#if>

										${fl.fun_name!}
									</label>
								</dt>
								<br/>
								<dd style="margin-left:30px; margin-top:10px;" >
									<#list funList as ll>
										<#if ll.fid == fl.id >
											<dl class="cl permission-list3">
												<dt class="funroles">
													<label class="cl">
													├ 
													<#if ll.checked??>
													<input type="checkbox" value="${ll.id}" name="user-menu-${fl_index!}-${ll_index!}" id="user-menu-${fl_index!}-${ll_index!}"  checked="checked" />
													<#else>
													<input type="checkbox" value="${ll.id}" name="user-menu-${fl_index!}-${ll_index!}" id="user-menu-${fl_index!}-${ll_index!}"  />
													</#if>
													
													${ll.fun_name!}
													</label>
												</dt>
												<dd style="margin-left:30px; margin-top:10px;" class="btroles">
													├ 
													<#list butList as dd>
														<#if dd.fun_id == ll.id >
															<label class="">
															<#if dd.checked??>
															<input type="checkbox" value="${dd.id}" name="user-menu-${fl_index!}-${ll_index!}-${dd_index!}" id="user-menu-${fl_index!}-${ll_index!}-${dd_index!}"  checked="checked" />
															<#else>
															<input type="checkbox" value="${dd.id}" name="user-menu-${fl_index!}-${ll_index!}-${dd_index!}" id="user-menu-${fl_index!}-${ll_index!}-${dd_index!}"  />
															</#if>
															
															${dd.but_name!}
															</label>
														</#if>
													</#list>
												</dd>
											</dl>
										</#if>
									</#list>
								</dd>
							 </dl>
							</#if>
						</#list>
						</#if>
					</dd>
					</dl>
			</div>
		</div>
		<!-- end -->
		<div class="row cl">
			<div class="col-9 col-offset-3">
				<input class="btn btn-primary radius" type="button" onclick="subForm()" value="&nbsp;&nbsp;提交&nbsp;&nbsp;">
			</div>
		</div>
	</form>
</div>
<script type="text/javascript">
	function subForm(){
		//后台请求保存数据
		var data = {};

		var funroles = "";
		var btroles = "";
		$('.funroles input:checkbox:checked').each(function() {
			funroles += $(this).val() + ",";
		});
		$('.btroles input:checkbox:checked').each(function() {
			btroles += $(this).val() + ",";
		});

		data.funroles = funroles;
		data.btroles = btroles;
		data.chId = $("#chId").val();
		$.axse("${basePath}/chPermission/addpermissrole.do", data , function(d){
			if(d.resultCode == 200){
				layer.msg('保存成功!', {
					icon : 1,
					time : 1000
				})
				var index = parent.layer.getFrameIndex(window.name);
				parent.$('.btn-refresh').click();
				parent.layer.close(index);
				// location.replace("${basePath}/chFunction/topermissrole.do?id="+data.chId);
			}else{
				$("#form-role-permiss")[0].reset();
				layer.msg('保存失败，请你检查数据!', {
					icon : 2,
					time : 1000
				});
			}
		},
		function(d){
			$("#form-role-permiss")[0].reset();   
			layer.msg('保存失败，请你检查数据!', {
				icon : 2,
				time : 1000
			});
		});
	}	
	
	$(function(){
		// 获取最顶级的复选框选项
		var top_input = $(".permission-list").find("dt").first().find("input:checkbox"); 
		$(".permission-list dt input:checkbox").click(function(){
			$(this).closest("dl").find("dd input:checkbox").prop("checked", $(this).prop("checked"));
		});

		$(".permission-list2 dt input:checkbox").click(function(){
			var list2_dt = $(this).parents(".permission-list").find(".permission-list2 dt").find("input:checked").length;
			if(list2_dt == 0){
				top_input.prop("checked", false);
			} else {
				top_input.prop("checked", true);
			}
		});
		
		$(".permission-list3 dt input:checkbox").click(function(){
			var list3_dt = $(this).parents(".permission-list2").find(".permission-list3 dt").find("input:checked").length;
			if(list3_dt == 0){
				$(this).parents(".permission-list2").find("dt").first().find("input:checkbox").prop("checked", false);
			} else {
				$(this).parents(".permission-list2").find("dt").first().find("input:checkbox").prop("checked", true);
			}
			var all_list3_dt = $(".permission-list3 dt").find("input:checked").length;
			if(all_list3_dt == 0){
				top_input.prop("checked", false);
			}else{
				top_input.prop("checked", true);
			}
		});

		$(".permission-list3 dd input:checkbox").click(function(){
			var list3_dd = $(this).parents(".permission-list2").find(".permission-list3 dd").find("input:checked").length;
			if(list3_dd > 0){
				$(this).parents(".permission-list3").find("dt").first().find("input:checkbox").prop("checked", true);
				$(this).parents(".permission-list2").find("dt").first().find("input:checkbox").prop("checked", true);
			}
			var all_list3_dd = $(".permission-list3 dd").find("input:checked").length;
			if (all_list3_dd > 0) {
				top_input.prop("checked", true);
			}
		});

		// $(".btroles input:checkbox").click(function(){
		// 	var list = $(".btroles").find("input:checked").length;
		// 	var list2_dd = $(this).parents(".permission-list").find(".permission-list2 dd").find("input:checked").length;
		// 	var list3_dd = $(this).parents(".permission-list").find(".permission-list3 dd").find("input:checked").length;
			
		// 	if($(this).prop("checked")){
		// 		$(this).closest("dl").find("dt input:checkbox").prop("checked", true);
		// 		top_input.prop("checked", true);
		// 		$(this).parents(".permission-list2").find("dt").first().find("input:checkbox").prop("checked", true);
		// 	}
		// 	else{
		// 		if(list == 0){
		// 			$(this).closest("dl").find("dt input:checkbox").prop("checked", false);
		// 			$(this).parents(".permission-list2").find("dt").first().find("input:checkbox").prop("checked", false);
		// 		}
		// 		if(list2_dd == 0){
		// 			top_input.prop("checked", false);
		// 			$(this).parents(".permission-list2").find("dt").first().find("input:checkbox").prop("checked", false);
		// 		}
		// 		if(list3_dd == 0){
		// 			$(this).parents(".permission-list2").find("dt").first().find("input:checkbox").prop("checked", false);
		// 		}
		// 	}
		// });
		
		window.onload = function(){
			var input = $(".permission-list2 input");
			if(input.prop("checked")) {
				top_input.prop("checked", true);
			}
		}
	});

</script>
</body>
</html>